const JsonResult = require('@/utils/httpResponse.unit')
const { apiGetTutorialCategory, apiGetTutorialList, apiGetTutorialDetail, apiGetTutorialChapterInfo, apiGetTutorialChapterComment } = require('@/http/tutorial')

class TutorialController {
  async getTutorialCategory (req, response) {
    try {
      const data = await apiGetTutorialCategory()
      JsonResult.success({
        req,
        data,
        response,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  async getTutorialList (req, response) {
    try {
      const categoryId = req.body.categoryId
      const data = await apiGetTutorialList(categoryId)
      JsonResult.success({
        req,
        data: data.list || [],
        response,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  async getTutorialDetail (req, response) {
    try {
      const tutorialId = req.body.tutorialId
      const data = await apiGetTutorialDetail(tutorialId)
      JsonResult.success({
        req,
        data,
        response,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  async getTutorialChapterInfo (req, response) {
    try {
      const { chapterUuid } = req.body
      const data = await apiGetTutorialChapterInfo(chapterUuid)
      JsonResult.success({
        req,
        data,
        response,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  async getTutorialChapterComment (req, response) {
    try {
      const { chapterUuid } = req.body
      const data = await apiGetTutorialChapterComment(chapterUuid)
      JsonResult.success({
        req,
        data,
        response,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }
}

module.exports = new TutorialController()
